package emt.friendtracking;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Data 
{
	private static final String TAG = Data.class.getSimpleName();

	static final int VERSION = 1; //1

	static final String DATABASE = "friendtracking.db";
	static final int DB_VERSION = 3;
	static final String TABLE = "friendtracking";
	
	public enum DIRECTION { INCOMING, OUTGOING};

	/**
	 * 
	 */
	static final String C_ID = "_id";
	public static final String C_DATE = "date";
	public static final String C_SOURCE = "source";
	public static final String C_SOURCE_NAME = "source_name";
	public static final String C_DIRECTION = "direction";
	public static final String C_LONGITUDE_ME = "longitude_me";
	public static final String C_LATITUDE_ME = "latitude_me";
	public static final String C_LONGITUDE_OTHER = "longitude_other";
	public static final String C_LATITUDE_OTHER = "latitude_other";
	
	public static final int R_SOURCE_NAME = 0;
	public static final int R_LONGITUDE_ME = 1;
	public static final int R_LATITUDE_ME = 2;
	public static final int R_LONGITUDE_OTHER = 3;
	public static final int R_LATITUDE_OTHER = 4;

	private static final String GET_ALL_ORDER_BY = C_DATE + " DESC";

	// DbHelper implementations
	class DbHelper extends SQLiteOpenHelper 
	{
		String TAGdb = DbHelper.class.getSimpleName();
		public DbHelper(Context context)
		{
			super(MyApplication.getAppContext(), DATABASE, null, VERSION);
			Log.i(TAGdb, "onCreate");
		}

		@Override
		public void onCreate(SQLiteDatabase db) 
		{
			Log.i(TAG, "Creating database: " + DATABASE);
			db.execSQL("drop table if exists " + TABLE);
			db.execSQL(" create table " + TABLE + " (" + C_ID + " INTEGER PRIMARY KEY, "
					+ C_DATE + " text, " + C_SOURCE + " text, " + C_SOURCE_NAME + " text, " 
					+ C_DIRECTION + " text, " + C_LONGITUDE_ME + " double, " + C_LATITUDE_ME + " double, " 
					+ C_LONGITUDE_OTHER + " double, " + C_LATITUDE_OTHER + " double)");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("drop  table if exists " + TABLE);
			this.onCreate(db);
		}
	}

	private final DbHelper dbHelper;

	public Data() 
	{
		this.dbHelper = new DbHelper(MyApplication.getAppContext());
		Log.i(TAG, "Initialized data"); //TODO "Initialized data"
	}

	public void close() 
	{
		this.dbHelper.close();
	}

	public Long insertOrIgnore(ContentValues values) 
	{
		Log.d(TAG, "insertOrIgnore on " + values);
		SQLiteDatabase db = this.dbHelper.getWritableDatabase();
		Long row = null;
		try 
		{
			row = db.insertWithOnConflict(TABLE, null, values,
					SQLiteDatabase.CONFLICT_IGNORE); 
		} 
		finally 
		{
			db.close();
		}
		return row;
	}

	/**
	 * 
	 * @return Cursor where the columns are _id, created_at, user, txt
	 */
	public Cursor getHistory() 
	{
		SQLiteDatabase db = this.dbHelper.getReadableDatabase();
		return db.query(TABLE, null, null, null, null, null, GET_ALL_ORDER_BY);
	}

	public Information getItemById(Long id)
	{
		SQLiteDatabase db = this.dbHelper.getReadableDatabase();
		
		Cursor cursor = db.query(TABLE, new String[]{ C_SOURCE_NAME, C_LONGITUDE_ME, C_LATITUDE_ME, C_LONGITUDE_OTHER, C_LATITUDE_OTHER, C_SOURCE},
				Data.C_ID+"=?", new String[]{id.toString()}, null, null, null);
		
		cursor.moveToFirst();
		
		Information i = new Information(cursor.getString(0), cursor.getDouble(1), cursor.getDouble(2), cursor.getDouble(3), cursor.getDouble(4), cursor.getString(5));
		cursor.close();
		
		return i;
	}
	
	public class Information
	{
		private String name, phoneNumber;
		private double long_me;
		private double lat_me;
		private double long_o;
		private double lat_o;
		
		private Information(String s, double a1, double a2, double a3, double a4, String phoneNo)
		{
			name = s;
			long_me = a1;
			lat_me = a2;
			long_o = a3;
			lat_o = a4;
			phoneNumber = phoneNo;
		}
		
		public String getUserName()
		{
			return name;
		}

		public double getLongitudeMe()
		{
			return long_me;
		}

		public double getLatitudeMe()
		{
			return lat_me;
		}
		
		public double getLongitudeOther()
		{
			return long_o;
		}

		public double getLatitudeOther()
		{
			return lat_o;
		}
		
		public String getPhoneNumber()
		{
			return phoneNumber;
		}
	}

	/**
	 * Deletes ALL the data
	 */
	public void delete() 
	{
		// Open Database
		SQLiteDatabase db = dbHelper.getWritableDatabase();

		// Delete the data
		db.delete(TABLE, null, null);

		// Close Database
		db.close();
	}
}
